﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetAttributeNames]
	-- Add the parameters for the stored procedure here
	@NOSQLCODE nvarchar(max),
	@GROUPATTRIBUTES nvarchar(max) = ''' '''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	CREATE Table #AttrList (
		AttributeName NVARCHAR(255),
		DataType NVARCHAR(50),
		Groupable BIT,
		Editable BIT
	)
	DECLARE @EntityType NVARCHAR(100)
	DECLARE @EntitySubType NVARCHAR(100)
	DECLARE @RowCount int = 0

--	DECLARE @NEWSQL nvarchar(max) = 'SELECT * INTO #tmp FROM (' + dbo.ReadDataFromDB_ConvertSQL(@NOSQLCODE,'','','','') + ') as a; ' + 'SELECT cols.name, ty.name FROM tempdb.sys.columns cols join sys.types ty on cols.system_type_id = ty.system_type_id WHERE [object_id] = OBJECT_ID(N''tempdb..#tmp'')'
DECLARE @NEWSQL nvarchar(max) = 'SELECT * INTO #tmp FROM (' + dbo.ConvertRafikiNOSQL(@NOSQLCODE + ' WHERE 0=1','',default,default,default) + ') as a; ' +
'INSERT INTO #AttrList ' +
'SELECT        cols.name AS AttributeName, dbo.DefinedDataTypes.RafikiDataType as DataType,' +
	'CASE WHEN cols.name IN (' + @GROUPATTRIBUTES + ') THEN 1 ELSE 0 END AS GROUPABLE,0 AS Editable
FROM            tempdb.sys.columns AS cols INNER JOIN
                         sys.types AS ty ON cols.system_type_id = ty.system_type_id CROSS JOIN
                         dbo.DefinedDataTypes
WHERE        (cols.object_id = OBJECT_ID(N''tempdb..#tmp'')) AND (dbo.DefinedDataTypes.SQLDataTypes LIKE ''%'' + ty.name + ''%'') ORDER BY column_id'

	EXECUTE( @NEWSQL)

	-- At this point, we have the list of attributes, now we check and see if it is a simple request for an entity or something more complex. 
	IF CHARINDEX('{',@NOSQLCODE) <> 0 AND CHARINDEX('}',@NOSQLCODE) <> 0 
			-- {} may be nested inside [], but [] will not be nested inside {}
			SET @EntityType = RTRIM(LTRIM(SUBSTRING(@NOSQLCODE,CHARINDEX('{',@NOSQLCODE)+1,
													CHARINDEX('}',@NOSQLCODE)-CHARINDEX('{',@NOSQLCODE)-1)))
	IF CHARINDEX('.',@EntityType) <> 0 BEGIN
		SET @EntitySubType = SUBSTRING(@EntityType,CHARINDEX('.',@EntityType)+1,LEN(@EntityType))
		SET @EntityType = SUBSTRING(@EntityType,1,CHARINDEX('.',@EntityType)-1)
	END ELSE 
		SET @EntitySubType = NULL

	IF @EntitySubType IS NULL
		SELECT @RowCount = COUNT(*) FROM DefinedEntityTypes WHERE @EntityType = EntityType
	ELSE
		SELECT @RowCount = COUNT(*) FROM DefinedEntitySubTypes WHERE @EntitySubType = EntitySubType AND @EntityType = EntityType

	IF @RowCount <> 0
		-- use the contents of the definedattributes table to update attrlist with potentially editable values.
		UPDATE AL
		SET AL.Editable = 1 
		FROM #AttrList AL INNER JOIN DefinedAttributes DA 
			ON DA.AttributeName = AL.AttributeName 
			AND @EntityType = DA.EntityType 
			AND (DA.EntitySubType IS NULL OR @EntitySubType = DA.EntitySubType)

	SELECT * FROM #AttrList

END